#ifndef CERTTABLEMODEL_H
#define CERTTABLEMODEL_H

#include <QAbstractTableModel>
#include <QObject>
#include <QList>

enum class SortType {
  Undefined,
  ByLogicalPosition,
  ByKeyUsage,
};

enum class X509Field {
  Subject,
  Issuer,
  ValidityFrom,
  ValidityTo,
  KeyUsage,
  FriendlyName,
  TotalCount,
};

typedef struct x509_st X509;

class CertTableModel : public QAbstractTableModel
{
  Q_OBJECT
public:
  enum ModelType {

  };
  class Delegate {
  public:
    virtual ~Delegate() {}
    virtual QList<X509* > GetCertificates(const QString& category) = 0;
    virtual QStringList GetCategories() = 0;
    void SetSortType(SortType sort_type) {
      sort_type_ = sort_type;
    }

    void SetModel(CertTableModel* model) {
      model_ = model;
    }

    virtual const QVector<QString>& GetKeyUsageName() = 0;
    virtual QStringList GetExtendedKeyUsage(X509* cert) = 0;

  protected:
    SortType sort_type_ = SortType::Undefined;
    CertTableModel* model_ = nullptr;
  };

  explicit CertTableModel(QObject *parent, Delegate* delegate);
  ~CertTableModel() override;
  void SetSortType(SortType sort_type);

  QStringList GetCategories() {
    return std::move(delegate_->GetCategories());
  }

  void SetCategory(const QString& category);

  // override QAbstractTableModel

  int columnCount(const QModelIndex &parent = QModelIndex()) const override;
  int rowCount(const QModelIndex &parent = QModelIndex()) const override;
  QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;


  QVariant headerData(int section,
                      Qt::Orientation orientation,
                      int role = Qt::DisplayRole) const override;


  X509* GetCert(int row) {
    return certs_[row];
  }


private:
  Delegate* delegate_ = nullptr;
  QList<X509* >           certs_;
};

#endif // CERTTABLEMODEL_H
